package org.brisling.oa.queryService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.oa.domain.OAConferenceroomApply;
import org.brisling.sysman.domain.Users;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <p>Title: QueryOAConferenceroomApplyService</p>
 * <p>Description: 会议室申请查询服务类</p>
 * <p>Company: tongking</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2016年7月9日 下午11:30:16
 */


//spring controller annotation
@RestController

//spring 业务模块映射地址，当前spring context 中必须确保唯一
@RequestMapping("/oaConferenceroomApply2/query")
public class QueryOAConferenceroomApplyService2 extends AbstractBaseQueryService<OAConferenceroomApply>{
	
	//自动匹配登录验证服务类
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	
	//自动匹配数据字典服务类
	@Autowired
	private QueryDictionaryService dicService;
	
	//科室代码静态常量
	private static final String DEPT_CODE = "apply_deptid";
	
	//查询权限等级静态常量，匹配该项的用户，将忽略科室及人员过滤条件进行查询
	private static final String QUERY_BIZ_ALL_CODE = "query_oa_all";
	
	/**
	 * 构造函数
	 */
	public QueryOAConferenceroomApplyService2(){
		//模块代码定义
		this.moduleName = ConstSysParam.MODULE_CONFERENCEROOM_APPLY;
		
		//子系统定义
		this.subSystem = ConstSysParam.SUBSYSTEM_OA;
		
		//业务表（视图）名称定义
		this.tableName =this.moduleName;
		
		//业务实体类定义
		this.clazz = OAConferenceroomApply.class;
		
		//查询过滤条件字段定义
		typeMap = this.getTypeMap(clazz);
		
		//排序字段定义
		orderMap.put("bizno",null);
		
		
	}
	
	//spring entityManager 定义，unitName与配置文件中相应配置项必须匹配
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	//业务模块主页面（列表页面）映射地址定义，返回业务模块主页面（列表页面）spring 视图
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	//业务模块编辑页面映射地址定义，返回业务模块编辑页面 spring 视图	
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}	
	
	@RequestMapping("/eventPage.action")
	public ModelAndView getEventPage(){
		return super.getBizPage(null, null, "eventPage");		
	}	
	
	/**
	 * 获取模块code名称对应的页面视图
	 * @param code	模块目录中的页面名称
	 * @return
	 */
	@RequestMapping("/getPage.action")
	public ModelAndView getBizPage(@RequestParam(value="code",required=false) String code){		
		
		return this.getBizPage(null,null,code);
	}
	//获取列表页面分页数据集方法
	@RequestMapping("/getListDomain.action")
	public ListDomain<OAConferenceroomApply> getListDomain(
			@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
	}
	
	//获取当前用户可操作（已签收，未签收）业务数据集方法
	@RequestMapping("/getOperateTaskListDomain.action")
	public ListDomain<OAConferenceroomApply> getUserCanoperateTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_can_operate");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		return super.getUserTaskCanOperateListDomain(userid,sql, conditionMap);
	}
	
	//获取当前用户已签收业务数据集
	@RequestMapping("/getSignedTaskListDomain.action")
	public ListDomain<OAConferenceroomApply> getUserSignedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_signed");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		return super.getUserSignedListDomain(userid,sql, conditionMap,null);
	}
	
	@RequestMapping("/getRoomListDomain.action")
	public ListDomain<OAConferenceroomApply> getRoomListDomain(
			@RequestBody(required=false) String body){	
		ListDomain<OAConferenceroomApply> domains= super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
		return domains;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/getRoomList.action")
	public List<OAConferenceroomApply> getRoomList(
			@RequestBody(required=false) String body){	
		Map<String,Object> bodyMap = this.getBodyParam(body);
		Map<String,String> paramMap = (Map<String, String>) bodyMap.get(ConstSysParam.CONDITION_MAP_KEY);
		List<OAConferenceroomApply> domains= super.getListByConditionAndOrder(paramMap, typeMap, orderMap,true,false);
		return domains;
	}
	/**
	 * 获取当前用户未签收数据集
	 * @param body
	 * @return
	 */
	@RequestMapping("/getUnsignedTaskListDomain.action")
	public ListDomain<OAConferenceroomApply> getUserUnsignedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_can_signed");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		return super.getUserUnsignedListDomain(userid,sql, conditionMap,null);
	}
	
	/**
	 * 获取当前业务已完成数据集
	 * @param body
	 * @return
	 */
	@RequestMapping("/getFinishedTaskListDomain.action")
	public ListDomain<OAConferenceroomApply> getUserFinishedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_finished");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, QUERY_BIZ_ALL_CODE))
			conditionMap = this.addDeptConditionToMap(userid,conditionMap, DEPT_CODE);
			
		return super.getUserFinishedListDomain(sql, conditionMap,null);
	}
	
	/**
	 * 获取当前已完成审批会议申请
	 * @param body
	 * @return
	 */
	@RequestMapping("/getFinishedListDomain.action")
	public ListDomain<OAConferenceroomApply> getFinishedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_finished");
		
		Map<String,Object> conditionMap = this.getBodyParam(body);		
			
		return super.getUserFinishedListDomain(sql, conditionMap,null);
	}
	
	/**
	 * 获取当前用户管理的数据集
	 * @param body
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getUserManagedTaskDomain.action")
	public ListDomain<OAConferenceroomApply> getUserManagedTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_managed_manageuser");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, QUERY_BIZ_ALL_CODE)){
			Users usr = SysConfig.getUsr(userid);
			if(this.isManageJob(userid)){
				conditionMap = this.addDeptConditionToMap(userid,conditionMap, DEPT_CODE);
			}else{
				Map<String,String> paramMap = (Map<String, String>) conditionMap.get(ConstSysParam.CONDITION_MAP_KEY);
				if(paramMap==null){
					paramMap = new HashMap<String,String>();
				}
				paramMap.put("user_id_", usr.getIdentify());
				conditionMap.put(ConstSysParam.CONDITION_MAP_KEY, paramMap);
				sql = dicService.getSqlByBizcode("flow_managed_normaluser"); 
			}
		}
		return super.getUserManagedListDomain(sql, conditionMap,null);
	}
	@RequestMapping("/getAllTaskListDomain.action")
	public ListDomain<OAConferenceroomApply> getAllTask(@RequestBody(required=false) String body){
		String sql = dicService.getSqlByBizcode("flow_all_task");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, QUERY_BIZ_ALL_CODE))
			conditionMap = this.addDeptConditionToMap(userid,conditionMap, DEPT_CODE);
			
		return super.getAllListDomain(sql, conditionMap,null);
	}
	
	/**
	 * 根据flow_state参数返回相应状态的当前业务数据集
	 * @param body
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getTaskListDomain.action")
	public ListDomain<OAConferenceroomApply> getTaskListDomain(@RequestBody(required=false) String body){
		
		
		ListDomain<OAConferenceroomApply> domains = new ListDomain<OAConferenceroomApply>();
		Map<String,Object> conditionMap = this.getBodyParam(body);
		if((conditionMap!=null) && (conditionMap.containsKey("condition"))){
			Map<String,String> cMap = (Map<String, String>) conditionMap.get(ConstSysParam.CONDITION_MAP_KEY);
			String _state = null;
			if((cMap!=null) && (cMap.containsKey("flow_state"))){
				_state = cMap.get("flow_state");				
			}else{
				_state = ConstSysParam.FLOW_CANOPERATE;
			}			
			switch(_state){
			case ConstSysParam.FLOW_CANOPERATE:
				domains = getUserCanoperateTask(body);
				break;
//			case ConstSysParam.FLOW_SIGNED:
//				domains = getUserSignedTask(body);
//				break;
//			case ConstSysParam.FLOW_UNSIGED:
//				domains = getUserUnsignedTask(body);
//				break;
			case ConstSysParam.FLOW_MANAGED:
				domains = getUserManagedTask(body);
				break;
			case ConstSysParam.FLOW_FINISHED:
				domains = getUserFinishedTask(body);
				break;
			case ConstSysParam.FLOW_all:
				domains = getAllTask(body);
				break;
			default:
				domains = getUserCanoperateTask(body);
				break;
			}
		}
		return domains;
	}
	
	
	/**
	 * 根据业务id查找单条业务记录
	 * @param id	业务id
	 * @return 业务记录 
	 */
	@RequestMapping("/getOne.action")
	public OAConferenceroomApply getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据业务唯一编码查找单条业务记录
	 * @param identify	业务唯一编码
	 * @return 业务记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public OAConferenceroomApply getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	/**
	 * 根据指定字段名及字段值查找记录
	 * @param field		字段名
	 * @param fieldVal	字段值
	 * @return
	 */
	@RequestMapping("/getOneByBizTable.action")
	public OAConferenceroomApply getOneByBizTable(@RequestParam(value="field",required=false) String field,
			@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByTable(field,fieldVal);
	}
	
	/**
	 * 根据业务字段查找单条业务信息
	 * @param field	字段名称
	 * @param fieldVal	字段值
	 * @return 业务记录
	 */
	@RequestMapping("/getOneByField.action")
	public OAConferenceroomApply getOneByField(@RequestParam(value="field",required=false) String field,
			@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByIdentify(field,fieldVal);
	}
	
	
	/**
	 * 返回流程记录流程实例的执行记录
	 * @param body	包含procInstId在内的参数信息
	 * @return
	 */
	
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<Object> getHistoricByProcInstId(@RequestBody(required=false) String body){
		
		String sql = dicService.getSqlByBizcode("flow_historics");				
		return super.getHistoricByProcInstId(body, sql);
	}
	public List<OAConferenceroomApply> getWindowUnSatisfy(){		
		
		String sql =dicService.getSqlByBizcode("duties_abnormal_OAConferenceroomApply");		
		
		try {
			return super.getClassedAllList(sql, OAConferenceroomApply.class);
		} catch (DangerousOperatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 	
		return null;
	}
}
